import os


class Couchdb():
    COUCHDB_HOSTS = 'http://127.0.0.1:5984'
    COUCHDB_DATABASES = (
        ('django_couchdb_utils_auth', 'http://127.0.0.1:5984/authdb'),
        ('django_couchdb_utils_sessions', 'http://127.0.0.1:5984/sessionsdb'),
        ('django_couchdb_utils_cache', 'http://127.0.0.1:5984/cachedb'),
    )
    INSTALLED_APPS = ()
    path = os.path.abspath(os.curdir)

    def __init__(self, host=None, port=None):
        if host and port:
            self.COUCHDB_HOSTS = '%s:%s' % (host, port)

    def updateEntities(self, path=None):
        dirs = os.listdir(self.path + path)
        if 'models.py' in dirs:
            self.addEntity(path)
        for dir in dirs:
            if dir.find('.') == -1:
                self.updateEntities(self.path + '/models/' + path + dir + '/')

    def addEntity(self, path):
        self.COUCHDB_DATABASES += ((path.replace('/', '.'),), '%s/%s' % (self.COUCHDB_HOSTS, path),)
        self.INSTALLED_APPS += (path.replace('/', '.'),)


